//
//  TXFComposeTextView.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/3.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

class TXFComposeTextView: UITextView {

    
    var composeFont: UIFont?{
    
        didSet{
        
            
            
        placeholderLabel.font = composeFont
        
        }
    
    
    }
    
    var placehoder: String?{
    
        didSet{
        
        placeholderLabel.text = placehoder
        
        }
    
    
    
    }
    
    
    
 override   init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    
    setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    func  setupUI(){
     //添加控件:
    addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            
            
            make.top.equalTo(self).offset(8)
            
            make.leading.equalTo(self).offset(5)
            
            make.width.equalTo(SCREENW-10)
            
            
        }
    
    //通过系统通知监听文字的变化
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    
    }
    
    func textViewTextDidChange(){
    
    //让他消失根据是否有占位数
    placeholderLabel.hidden = self.hasText()
        
        
    }
    
    //移除通知
    
    deinit{
    
    
    NSNotificationCenter.defaultCenter().removeObserver(self)
    
    }
    
    //懒加载一个Label
    
    private lazy var placeholderLabel: UILabel = {
    
    let lab = UILabel()
        
    lab.textColor = UIColor.grayColor()

    lab.numberOfLines = 0
        
    lab.font = UIFont.systemFontOfSize(12)
        
    //占位字
//    lab.text = "关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博关注我的微博"
        
    
    return lab
    
    }()
    
}
